package com.dora.basic.basic;

import org.junit.jupiter.api.Test;

/**
 * 外部类
 *
 * @author hanchen
 * @date 2024/02/20
 */
public class OuterClass {
    private int outerField;

    /**
     * 成员内部类
     */
    public class InnerClass {
        public void display() {
            System.out.println("InnerClass display: " + outerField);
        }
    }

    /**
     * 静态内部类
     */
    public static class StaticInnerClass {
        public void display() {
            System.out.println("StaticInnerClass display");
        }
    }

    /**
     * 局部内部类
     */
    @Test
    public void localInnerClassTest() {
        final int localVar = 10;

        class LocalInnerClass {
            public void display() {
                System.out.println("Local inner class: " + localVar);
            }
        }

        LocalInnerClass inner = new LocalInnerClass();
        inner.display();
    }

    /**
     * 匿名内部类测试
     */
    @Test
    public void anonymousInnerClassTest() {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                System.out.println("Anonymous inner class: running...");
            }
        };

        Thread thread = new Thread(runnable);
        thread.start();
    }


    @Test
    public void testRun() {
        OuterClass outerObject = new OuterClass();

        // 创建成员内部类对象
        OuterClass.InnerClass innerObject = outerObject.new InnerClass();
        // 输出: InnerClass display: 0
        innerObject.display();

        // 创建静态内部类对象
        OuterClass.StaticInnerClass staticInnerObject = new OuterClass.StaticInnerClass();
        // 输出: StaticInnerClass display
        staticInnerObject.display();
    }
}
